package com.example.xd.hotfixdemo;

import android.content.Context;
import android.support.annotation.Keep;
import android.util.Log;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * @auther XuDan on 2018/10/18.
 */
public class SophixStubApplication extends SophixApplication {
    @Keep
    @SophixEntry(MyRealApplication.class)
    static class RealApplicationStub {
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//         如果需要使用MultiDex，需要在此处调用。
//         MultiDex.install(this);
        initSophix();
        SophixManager.getInstance().queryAndLoadNewPatch();
    }

    private void initSophix() {
        String appVersion = "0.0.0";
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setAppVersion(appVersion)
//                .setSecretMetaData("25165801","e87050e94a5f3cfb790e0b9fdc64140e","MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQhLgK4KVxjBKgA/ZrHecUCpiuZ++SCNcGxZj36PWfx+S2475Fhf3FvreADbXndYqu52r7uk+rjgVhdHK3qe5+XvUTQzQ74lJ50DXX1XK4NmG9wUPaNnDvpr/HM/sZJnN30BqyRTpAWgJjNs9D+FJUrG3eX9HSoSeD7m33O1KnrfkuK5ywW9QKQe0xgZhcT6fP5x2IrkHpf8FiZACcntyAec0CaaWH59CuYtJzGisl3hvLmUzDHQgJYnH0e6hwofYISEgFJQfKxS4cK2I7yeTDrXoOOJ0gi4M9mfHTXaCyWsF39FXSnoyVgO/l+ix0MbmOzE5Q4zIkTnpYPJSElIZ7AgMBAAECggEARUrNb7G8wc+2knCz8zI4PLzD7kO5c5y3dPDDYoKZWs4eVK6n6W/QeMLz+vsX7+3E3A3rlt0DHC+RVuPdqUBaMM9Y1K6RWfEvOGbjmiQmyDqS7MfAv90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/wW9e9cHY4k9cXHZhDzAPpTjIhVo=")
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.i("xd---", "成功");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // 如果需要在后台重启，建议此处用SharePreference保存状态。
                            Log.i("xd---", "失败");
                        }
                    }
                }).initialize();
    }
}
